
'use strict';

const BaseService = require("./base");


class MessageService extends BaseService{
    // 根据 用户id 获取消息列表 （公告+评论回复+审核消息）
    async getMsgByUser(body){
        const {userId} = body
        const res = await this.app.model.Message.findAll({
            where:{user_id:[userId,0],isShow:1},
            attributes:{exclude:['delete_time',]}
        }) 
        return res
    }

    // 生成消息
    async createMsg(body){
        const params = this.app._.cloneDeep(body)
        params.create_time = new Date()
        params.isRead = 0
        params.isShow = 1
        if(!params.create_id){
            params.create_id = 0
        }
        if(!params.article_id){
            params.article_id = 0
        }
        if(!params.user_id){
            params.user_id = 0
        }
        return await this.app.model.Message.create(params)
    }

    // 修改是否已读状态 (用户)
    async changeReadStatus(body){
        const {ids,isRead} = body
        await this.app.model.Message.update({isRead},{
            where:{id:ids}
        })
    }

    // 用户删除消息 控制isShow即可
    async UserDeleteMsg(body){
        const {ids} = body
        await this.app.model.Message.update({isShow:0},{
            where:{id:ids}
        })
    }

    // 获取公告列表
    /*
        //筛选项：
        创建者 create_id
        创建时间 开始时间startTime 结束时间endTime 
        关键词searchWord 
        当前页currentPage
        每页条数pageSize 
    */
    async getNoticeMsg(body){
       const params = this.app._.cloneDeep(body);
       if(params.all){
        const row = await this.app.model.Message.findAll({
            attributes:{exclude:['delete_time']},
            where:{type:1}
        })
        return row
       }else{
        const {count,rows} = await this.app.model.Message.findAndCountAll({
            distinct:true,
            where:this.queryWhere(params),
            attributes:{exclude:['delete_time']}, 
            // order:['create_time','DESC'],
            ...this.pageParam(params)
        });
        return this.paginate(count, rows,params);
       }
    }
    // 筛选项
    queryWhere(params){
        const {Op} = this.app.Sequelize
        const { create_id,sumTime,searchWord,isNotice} = params
        const startTime = sumTime[0]
        const endTime = sumTime[1]
        const where = {}
        if(isNotice){
            where.type = 1
        }
        if(searchWord!==''){
            where[Op.and] = {content:{[Op.like]:`%${searchWord}%`}}
        }
        if(startTime!=='' && endTime!==''){
            where[Op.and] = { create_time:{[Op.between]:[startTime,endTime]} }
        }
        if(create_id){
            where.create_id = create_id
        }
        return where
    }

    // 创建公告
    async createNoticeMsg(body){
        const params = this.app._.cloneDeep(body);
        params.type = 1
        params.isShow = 1
        params.isRead = 0
        params.user_id = 0
        params.article = 0
        params.create_time = new Date()
        return await this.app.model.Message.create(params)
    }

    // 编辑公告
    async updateNoticeMsg(body){
        const {id, create_id,content} = body
        await this.app.model.Message.update({ create_id,content,update_time:new Date() },{
            where:{
                id
            }
        })
    }

    // 管理员删除公告
    async consoleDelMsg(body){
        const { Op } = this.app.Sequelize;
        await this.app.model.Message.destroy({
          where:{
            id:{
              [Op.in]:body.ids
            }
          }
        })
    }
}


module.exports = MessageService;